﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.zhongjh.Library.Configuration;

namespace com.zhongjh.Library.Database
{
    /// <summary>
    /// 数据库信息实体类
    /// </summary>
    public class DbInfoEntity
    {

        private EDbType m_DbType = EDbType.Access;
        /// <summary>
        /// 数据库类型
        /// </summary>
        public EDbType DbType
        {
            get { return m_DbType; }
            set { m_DbType = value; }
        }

        private string m_DbServerNameOrPath = string.Empty;
        /// <summary>
        /// 数据库服务器名
        /// </summary>
        public string DbServerNameOrPath
        {
            get { return m_DbServerNameOrPath; }
            set { m_DbServerNameOrPath = value; }
        }

        private string m_DbName = string.Empty;
        /// <summary>
        /// 数据库名
        /// </summary>
        public string DbName
        {
            get { return m_DbName; }
            set { m_DbName = value; }
        }

        private string m_DbUser = string.Empty;
        /// <summary>
        /// 用户
        /// </summary>
        public string DbUser
        {
            get { return m_DbUser; }
            set { m_DbUser = value; }
        }

        private string m_DbPassword = string.Empty;
        /// <summary>
        /// 密码
        /// </summary>
        public string DbPassword
        {
            get { return m_DbPassword; }
            set { m_DbPassword = value; }
        }

        private string m_ConnectionString = string.Empty;
        /// <summary>
        /// 连接串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return ConnectionStringProcess();
            }
            set { m_ConnectionString = value; }
        }

        private bool m_IsLog = false;
        /// <summary>
        /// 是否日志输出
        /// </summary>
        public bool IsLog
        {
            get { return m_IsLog; }
            set { m_IsLog = value; }
        }

        private string m_ConfigFildPath = string.Empty;
        /// <summary>
        /// 数据库配置键
        /// </summary>
        public string DbConfigKey
        {
            get { return m_ConfigFildPath; }
            set { m_ConfigFildPath = value; }
        }

        private bool m_IsError = false;
        /// <summary>
        /// 是否错误输出
        /// </summary>
        public bool IsError
        {
            get { return m_IsError; }
            set { m_IsError = value; }
        }

        #region 公有方法

        public DbInfoEntity()
        {
            this.DbType = EDbType.Sql;
            this.DbServerNameOrPath = string.Empty;
            this.DbName = string.Empty;
            this.DbUser = string.Empty;
            this.DbPassword = string.Empty;
            this.DbConfigKey = string.Empty;
        }

        /// <summary>
        /// 数据库信息实体类构造函数
        /// </summary>
        /// <param name="p_strDbServerNameOrPath">数据库名或路径</param>
        /// <param name="p_strDbName">数据库名</param>
        /// <param name="p_strDbUser">用户名</param>
        /// <param name="p_strDbPassword">密码</param>
        /// <param name="p_EDbDrive">数据库驱动</param>
        /// <param name="p_EDbType">数据库类型</param>
        /// <returns></returns>
        public DbInfoEntity(string p_strDbServerNameOrPath, string p_strDbName, string p_strDbUser, string p_strDbPassword, EDbType p_EDbType)
        {
            this.DbServerNameOrPath = p_strDbServerNameOrPath;
            this.DbName = p_strDbName;
            this.DbUser = p_strDbUser;
            this.DbPassword = p_strDbPassword;
            this.DbType = p_EDbType;
            this.DbConfigKey = string.Empty;
        }

        public DbInfoEntity(ConfigItem p_configItem)
        {
            if (p_configItem.GetItem("m_DbType") != null)
            {
                switch (p_configItem.GetItem("m_DbType").Value.ToLower())
                {
                    case "access":
                        this.DbType = EDbType.Access;
                        break;
                    case "mysql":
                        this.DbType = EDbType.MySql;
                        break;
                    case "oracle":
                        this.DbType = EDbType.Oracle;
                        break;
                    case "sql":
                        this.DbType = EDbType.Sql;
                        break;
                }
            }
            this.DbServerNameOrPath =
                p_configItem.GetItem(DbConfigField.m_DbServerNameOrPath) == null ? string.Empty : p_configItem.GetItem(DbConfigField.m_DbServerNameOrPath).Value;
            this.DbName =
                p_configItem.GetItem(DbConfigField.m_DbName) == null ? string.Empty : p_configItem.GetItem(DbConfigField.m_DbName).Value;
            this.DbUser =
                p_configItem.GetItem(DbConfigField.m_DbUser) == null ? string.Empty : p_configItem.GetItem(DbConfigField.m_DbUser).Value;
            this.DbPassword =
                p_configItem.GetItem(DbConfigField.m_DbPassword) == null ? string.Empty : p_configItem.GetItem(DbConfigField.m_DbPassword).Value;
            this.ConnectionString =
                p_configItem.GetItem(DbConfigField.m_ConnectionString) == null ? string.Empty : p_configItem.GetItem(DbConfigField.m_ConnectionString).Value;
            if (p_configItem.GetItem(DbConfigField.m_IsLog) != null)
            {
                this.IsLog =
                    p_configItem.GetItem(DbConfigField.m_IsLog).Value == "1" ? true : false;
            }
            if (p_configItem.GetItem(DbConfigField.m_IsError) != null)
            {
                this.IsError =
                    p_configItem.GetItem(DbConfigField.m_IsError).Value == "1" ? true : false;
            }
        }

        #endregion 公有方法

        #region 私有方法

        private string ConnectionStringProcess()
        {
            string result = string.Empty;

            result = DbCSEntity.GetConnectionString(DbType, m_ConnectionString);
            if (result.Length > 2 && @":\".Equals(result.Substring(1, 2).ToString()))
            {
                if (m_DbServerNameOrPath.Length > 0 && m_DbServerNameOrPath.Substring(m_DbServerNameOrPath.Length - 1, 1) != @"\")
                    m_DbServerNameOrPath += @"\";
            }
            else
            {
                result = result.Replace("{" + DbConfigField.m_DbServerNameOrPath.Remove(0, 2) + "}", m_DbServerNameOrPath);
            }
            result = result.Replace("{" + DbConfigField.m_DbName.Remove(0, 2) + "}", m_DbName);
            result = result.Replace("{" + DbConfigField.m_DbUser.Remove(0, 2) + "}", m_DbUser);
            result = result.Replace("{" + DbConfigField.m_DbPassword.Remove(0, 2) + "}", m_DbPassword);

            return result;
        }

        #endregion //私有方法
    }
}
